import {Command} from '../constant';
import Taro from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let {userInfo, selectDistance} = getData().main;
      this.findUserInfo(userInfo.id, selectDistance.id);
      this.findUnitName(userInfo.houseId);
      this.findGateList(selectDistance.id);
      this.findCustomerblacklist(userInfo.phone);
    },
    /**
     * 查询黑名单
     */
    async findCustomerblacklist(phone) {
      let result = await api.mobileConfigController.findCustomerblacklist({customerMobile: phone});
      Taro.setStorageSync('blacklistModel', result.content.length > 0 ? true : false);
      action.commonChange('main.blacklistModel', result.content.length > 0 ? true : false);
    },
    /**
     * 查询用户人脸信息
     */
    async findUserInfo(id, communityId) {
      const params = {
        id,
        communityId,
      };
      let res = await api.openDoorController.findUserInfo(params);
      if (res.code == '200' && res.data.facePictureUrl) {
        action.commonChange([
          {
            paths: 'main.facePictureUrl',
            value: res.data.facePictureUrl,
          },
        ]);
      }
    },
    /**
     * 人脸信息采集
     */
    async uploadUserPicture(pictureUrl) {
      let userInfo = Taro.getStorageSync('b2b-wechat@login').user;
      let selectDistance = Taro.getStorageSync('selectDistance');
      const params = {
        pictureUrl,
        userId: userInfo.id,
        houseId: userInfo.houseId,
        communityId: userInfo.communityId,
      };
      let res = await api.openDoorController.uploadUserPicture(params);
      if (res.success) {
        Taro.showToast({
          title: '上传成功！',
          icon: 'none',
          duration: 2000,
        });
        this.findUserInfo(userInfo.id, selectDistance.id);
      } else {
        Taro.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        });
      }
    },
    /**
     * 查询单元门接口
     */
    async findUnitName(houseId) {
      const params = {
        houseId,
      };
      let res = await api.openDoorController.findUnitName(params);
      if (res.code == '200') {
        action.commonChange([
          {
            paths: 'main.unitDoorList',
            value: res.data,
          },
        ]);
      }
    },
    /**
     * 查询大门接口
     */
    async findGateList(communityId) {
      const params = {
        communityId,
      };
      let res = await api.openDoorController.findGateList(params);
      if (res.code == '200') {
        action.commonChange([
          {
            paths: 'main.doorList',
            value: res.data,
          },
        ]);
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageOpenDoorMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
